package com.changhai.diabetes.service.impl;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.changhai.diabetes.util.StringConvertor;
import com.changhai.diabetes.model.InterPatientGroup;
import com.changhai.diabetes.model.InterPatientUser;
import com.changhai.diabetes.model.LabPancreatCtai;
import com.changhai.diabetes.model.LabPancreatOgtt;
import com.changhai.diabetes.model.LabPancreatYidaosu;
import com.changhai.diabetes.model.Patient;
import com.changhai.diabetes.model.QueryDTO;
import com.changhai.diabetes.model.ResearchGroup;
import com.changhai.diabetes.model.SessionUser;
import com.changhai.diabetes.model.page.Page;
import com.changhai.diabetes.model.page.Pagination;
import com.changhai.diabetes.service.PatientService;
import com.changhai.diabetes.util.Constants;
import com.opensymphony.xwork2.ActionContext;

/**
 * 患者信息
 * 
 * @author Andy
 * 
 */
public class PatientServiceImpl extends CommonServiceImpl implements PatientService {

	public Page<?> searchQuery(QueryDTO queryDTO) {
		List<Map> list = new ArrayList();

		SessionUser sUser = (SessionUser) ActionContext.getContext().getSession().get(Constants.SESSION_USER_KEY);
		if (sUser != null) {
			Map<String, Number> map = new HashMap<String, Number>();
			if (sUser.getAdminType() == 0) {
				map.put("userId", sUser.getId());
			}
			List<Long> patientIds = this.getList(Constants.DS_CHANGHAI, "interPatientUser.getByUserId", map); // 获取属于该医院的患者编号
			if (patientIds != null && patientIds.size() > 0) {
				StringBuilder pid = new StringBuilder();
				Long gid = (Long) queryDTO.getConditions().get("researchgroupid");
				Map<String, Long> m = new HashMap<String, Long>();

				for (Long id : patientIds) {
					if (gid != null && gid >= 0) {
						m.put("pid", id);
						m.put("gid", gid);
						InterPatientGroup ipg = (InterPatientGroup) this.getObject(Constants.DS_CHANGHAI,
								"interPatientGroup.checkGroupPatient", m);
						if (ipg != null && ipg.getSeqid() > 0) {
							pid.append(String.valueOf(id)).append(",");
						} else {
							pid.append("0,");
						}
					} else {
						pid.append(String.valueOf(id)).append(",");
					}
				}
				queryDTO.getConditions().put("patientIds", StringConvertor.getIds(pid.toString()));
				list = this.getList(Constants.DS_CHANGHAI, "patient.searchQuery", queryDTO.getConditions()); // 获取属于该医院的患者信息
			}
		}
		return new Pagination((Integer) this.getObject(Constants.DS_CHANGHAI, "patient.countQuery",
				queryDTO.getConditions()), list, queryDTO.getConditions());
	}

	public Patient searchPatientById(Long patientId) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "patient.getByPrimaryKey", patientId);
		if (o != null && o instanceof Patient) {
			return (Patient) o;
		} else {
			return null;
		}
	}

	public void saveEditPatient(Patient patient) {
		if (patient != null) {
			this.updateObject(Constants.DS_CHANGHAI, "patient.updateByPrimaryKey", patient);
		}
	}

	public Long saveNewPatient(Patient patient) throws Exception {
		Integer patientid = 0;
		if (patient != null) {
			// 入系统时间
			Date now = new Date();
			DateFormat df = DateFormat.getDateTimeInstance();
			String start = df.format(now);
			patient.setStarttime(start);
			patientid = this.insertObjectID(Constants.DS_CHANGHAI, "patient.insert", patient);

			// 和医院的关联
			SessionUser sUser = (SessionUser) ActionContext.getContext().getSession().get(Constants.SESSION_USER_KEY);
			if (patientid > 0) {
				InterPatientUser interPatientUser = new InterPatientUser();
				interPatientUser.setPatientid(new Long(patientid));
				interPatientUser.setUserid(sUser.getId());
				this.insertObjectID(Constants.DS_CHANGHAI, "interPatientUser.insert", interPatientUser);
			}
		}
		return new Long(patientid);
	}

	public void delPatient(Long patientId) throws Exception {
		this.deleteObject(Constants.DS_CHANGHAI, "interPatientUser.deletePatient", patientId);
		Long gid = (Long) this.getObject(Constants.DS_CHANGHAI, "interPatientGroup.getGroupIdByPatientId", patientId);
		if (gid != null && gid > 0) {
			ResearchGroup group = (ResearchGroup) this.getObject(Constants.DS_CHANGHAI, "researchGroup.getByGroupId",
					gid);
			if (group != null) {
				Integer count = group.getPatientcount();
				if (count != null && count > 0) {
					group.setPatientcount(count - 1);
					this.updateObject(Constants.DS_CHANGHAI, "researchGroup.updateByPrimaryKey", group);
				}
			}
			this.deleteObject(Constants.DS_CHANGHAI, "interPatientGroup.deletePatient", patientId);
		}

		this.deleteObject(Constants.DS_CHANGHAI, "bear.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "concurrentResult.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "drugSuifang.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "drugSurvey.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "drugTreatment.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "family.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "labBlood.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "labGanshen.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "labLipids.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "labOther.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "labPancreatCtai.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "labPancreatOgtt.deletePatient", patientId);
 
		this.deleteObject(Constants.DS_CHANGHAI, "labPancreatCtai.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "labPancreatOgtt.deletePatient", patientId); 
		this.deleteObject(Constants.DS_CHANGHAI, "labPancreatYidaosu.deletePatient", patientId);
		
		this.deleteObject(Constants.DS_CHANGHAI, "labPhysical.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "labSugar.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "labUrine.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "nutritionSuifang.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "nutritionSurvey.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "nutritionTreatment.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "personal.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "physicalSurveyResult.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "qualitySurveyResult.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "sportsSuifang.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "sportsSurvey.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "sportsTreatment.deletePatient", patientId);
		this.deleteObject(Constants.DS_CHANGHAI, "symptomsSurveyResult.deletePatient", patientId);

		this.deleteObject(Constants.DS_CHANGHAI, "patient.deleteByPrimaryKey", patientId);
	}
}
